package com.atguigu.sms.controller.api;

import com.aiguigu.commonutils.R;
import com.aiguigu.commonutils.RandomUtils;
import com.aiguigu.commonutils.RegexValidateUtils;
import com.aiguigu.commonutils.ResponseEnum;
import com.atguigu.servicebase.exceptionhandler.Assert;
import com.atguigu.sms.client.CoreUserInfoClient;
import com.atguigu.sms.service.SmsService;
import com.atguigu.sms.utils.SmsProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
//@CrossOrigin
@Api(tags = "短信服务")
@RequestMapping("/api/sms")
public class ApiSmsController {

    @Resource
    private SmsService smsService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private CoreUserInfoClient coreUserInfoClient;



    @ApiOperation("发送验证码")
    @GetMapping("/sendSms/{phone}")
    public R sendSms(
            @ApiParam(value = "手机号",required = true)
            @PathVariable String phone
    ){
        // 1.判断手机号不为空
        Assert.notEmpty(phone, ResponseEnum.MOBILE_NULL_ERROR);
        // 2.判断手机号是否符合规则
        Assert.isTrue(RegexValidateUtils.checkCellphone(phone), ResponseEnum.MOBILE_ERROR);

        // 判断手机号是否已经注册
        boolean result  = coreUserInfoClient.isExist(phone);
        log.info("result = " +result);
        Assert.isTrue(result == false, ResponseEnum.MOBILE_EXIST_ERROR);

        // 3.生成验证码
        String code = RandomUtils.getSixBitRandom();
        // 4.创建模板map存储code
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",code );




        // 5.发送短信
        smsService.send(phone, SmsProperties.TEMPLATE_CODE,map);
        // 6.将验证码存储redis
        redisTemplate.opsForValue().set("api:sms:code:"+phone,code,5, TimeUnit.MINUTES);
        return R.ok().message("短信发送成功");
    }


    @ApiOperation("模拟验证码")
    @GetMapping("/sendRandom/{mobile}")
    public R sendRandom(
            @ApiParam(value = "手机号",required = true)
            @PathVariable String mobile
    ){

        // 模拟以及发送过验证码进行判断防止重复发送
        String o = (String) redisTemplate.opsForValue().get("api:sms:code:" + mobile);
        // 通一手机号短信发送频繁,给出提示
        Assert.isNull(o,ResponseEnum.ALIYUN_SMS_LIMIT_CONTROL_ERROR);

        // 1.判断手机号不为空
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);

        // 2.判断手机号是否符合规则
//        Boolean b = RegexValidateUtils.checkCellphone(mobile);
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile),ResponseEnum.MOBILE_ERROR);

        // 判断手机号是否已经注册
        boolean result  = coreUserInfoClient.isExist(mobile);
        log.info("result = " +result);
        Assert.isTrue(result == false, ResponseEnum.MOBILE_EXIST_ERROR);


        // 3.生成验证码
        String code = RandomUtils.getSixBitRandom();
        // 4. 假设发送
        smsService.sendRandom(mobile,code);

        // 6.将验证码存储redis
        redisTemplate.opsForValue().set("api:sms:code:"+mobile,code,5, TimeUnit.MINUTES);
        return  R.ok().message("短信发送成功");
    }
}
